package com.hw.one.dam2.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hw.one.core.service.BaseService;
import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.config.DamConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.dao.ICityAirDao;
import com.hw.one.dam2.dao.IRegionSiteDao;
import com.hw.one.dam2.dao.ISmallAirDao;
import com.hw.one.dam2.dao.ISourceDataDao;
import com.hw.one.dam2.dao.IWeatherDao;
import com.hw.one.dam2.ledVideo.EnvDataPO;
import com.hw.one.ext.util.UserUtil;

@Service
public class WeatherService extends BaseService implements IWeatherService {
	@Autowired
	private IWeatherDao weatherDao;

	@Autowired
	private IRegionSiteDao regionSiteDao;

	@Autowired
	private ICityAirDao cityAirDao;

	@Autowired
	private ISmallAirDao smallAirDao;

	@Autowired
	private ISourceDataDao sourceDataDao;


	private static final DamConfig damConfig = DamConfig.getInstance();

	private static String xdmlGpoint = damConfig.get("GPOINT_XDML");
	private static String xdmlSmall = damConfig.get("SMALL_XDML");
	private static String hjyKey = damConfig.get("HJY_KEY");
	private static String gpointCode = damConfig.get("GPOINT_XDML_CODE");
	private static double gpointPm10 = Double.parseDouble(damConfig.get("PERCENT_PM10"));
	private static double gpointPm25 = Double.parseDouble(damConfig.get("PERCENT_PM2_5"));
	private static double gpointSo2 = Double.parseDouble(damConfig.get("PERCENT_SO2"));
	private static double gpointNo2 = Double.parseDouble(damConfig.get("PERCENT_NO2"));
	private static double gpointCo = Double.parseDouble(damConfig.get("PERCENT_CO"));
	private static double gpointO3 = Double.parseDouble(damConfig.get("PERCENT_O3"));

	private static String lastMonitorTime;

	@Override
	public MsgResult findCityWeatherForecastByRegionCode(HttpServletRequest request) throws Exception {
		// 当前登录用户所属区域
		Map<String, Object> region = UserUtil.getRegion();
		String userRegionCode = (String) region.get("regionCode");
		// 查询天气预报的城市编码
		String cityCode = null;
		/**
		 * 根据当前登录用户区域，查询该区域的上级区域
		 */
		List<Map<String, Object>> parentRegionList = regionSiteDao.findParentRegionInfoByRegionCode(userRegionCode);
		// 把登录用户所属省份、和市编码，读取出来
		for (Map<String, Object> parentRegionMap : parentRegionList) {
			String retionType = String.valueOf(parentRegionMap.get("regionType"));
			// 市级名称
			if ("20".equals(retionType)) {
				cityCode = String.valueOf(parentRegionMap.get("regionCode"));
				break;
			}
		}

		Object weatherForecast = weatherDao.findLast7DayWeatherForecast(cityCode, "air_envi_weather_forecasetDay");

		return MsgResult.okResult(weatherForecast);
	}

	@Override
	public Object findCityLiveWeatherByRegionCode(Map<String, Object> paraMap) throws Exception {
		// 当前登录用户所属区域
		Map<String, Object> region = UserUtil.getRegion();
		String userRegionCode = (String) region.get("regionCode");
		// 查询天气预报的城市编码
		String cityCode = null;
		/**
		 * 根据当前登录用户区域，查询该区域的上级区域
		 */
		List<Map<String, Object>> parentRegionList = regionSiteDao.findParentRegionInfoByRegionCode(userRegionCode);
		// 把登录用户所属省份、和市编码，读取出来
		for (Map<String, Object> parentRegionMap : parentRegionList) {
			String retionType = String.valueOf(parentRegionMap.get("regionType"));
			// 市级名称
			if ("20".equals(retionType)) {
				cityCode = String.valueOf(parentRegionMap.get("regionCode"));
				break;
			}
		}
		Object cityLiveWeatherObj = weatherDao.findCityLiveWeatherByRegionCode(cityCode);
		return cityLiveWeatherObj;
	}

	@SuppressWarnings("unchecked")
	@Override
	public EnvDataPO findCityLiveWeatherByRegionCodeNoSession(Map<String, Object> paraMap) throws Exception {
		String siteCode = (String) paraMap.get("siteCode");

		Map aqiInfoMap = sourceDataDao.findScCollectMin5NewMinuteOneDataBySnCode("B518091061");
		Map<String, Object> monitorData = (Map<String, Object>) aqiInfoMap.get("MONITORDATA");

		Integer pm25 = (Integer) (monitorData.get("CTPM25")==null?0:monitorData.get("CTPM25"));
		Integer pm10 = (Integer) (monitorData.get("CTPM10")==null?0:monitorData.get("CTPM10"));
		Integer so2 = (Integer) (monitorData.get("CTSO2")==null?0:monitorData.get("CTSO2"));
		Integer no2 = (Integer) (monitorData.get("CTNO2")==null?0:monitorData.get("CTNO2"));
		Integer o3 = (Integer) (monitorData.get("CTO3")==null?0:monitorData.get("CTO3"));
		Integer co = (Integer) (monitorData.get("CTCO")==null?0:monitorData.get("CTCO"));


		String rectime = aqiInfoMap.get("MONITORTIME")==null?"":(String)aqiInfoMap.get("MONITORTIME");

		// 获取日历实例并赋予当前时间
		Calendar cNow = Calendar.getInstance();
		// 获取日历实例并赋予当前时间
		String[] weeks = new String[]{"日","一","二","三","四","五","六"};

		if(!"".equals(rectime)){
			cNow.setTime((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(rectime));
		}

		rectime = cNow.get(Calendar.DATE)+"日"+cNow.get(Calendar.HOUR_OF_DAY)+"时";
		String aqiLevelStr = "";
		EnvDataPO curEnvDataPO = new EnvDataPO();
//		curEnvDataPO.setPollutionLevel(aqiLevelStr);
//		curEnvDataPO.setTodayPollutionLevel(aqiLevelStr);
		curEnvDataPO.setPM10Concentration(pm10+"μg");
		curEnvDataPO.setPM2_5Concentration(pm25+"μg");
		curEnvDataPO.setSO2Concentration(so2 + "μg");
		curEnvDataPO.setNO2Concentration(no2+"μg");
		curEnvDataPO.setO3Concentration(o3+"μg");
		curEnvDataPO.setCOConcentration(co+"mg");
		curEnvDataPO.setPubDate(rectime);
		cNow = Calendar.getInstance();
		// 发布时间
		curEnvDataPO.setDayAndWeek(new SimpleDateFormat("MM月dd日").format(cNow.getTime())+" 星期" + weeks[cNow.get(Calendar.DAY_OF_WEEK)-1]);
		// 明日污染程度

		return curEnvDataPO;
	}

	@Override
	public Map<String, Object> findSiteMonitorUpdateDateBySnCode(Map<String, Object> map) {
//
		String siteCode = (String) map.get("siteCode");

		Map aqiInfoMap = smallAirDao.findSmallAirNewMinuteOneDataBySiteCode(siteCode);
		return regionSiteDao.findSiteMonitorUpdateDateBySnCode(map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public EnvDataPO findCityLiveWeatherByRegionCodeTwo(Map<String, Object> paraMap) throws Exception {
		String siteCode = (String) paraMap.get("siteCode");
		Map smallData = sourceDataDao.findScreenDataNewMinBySNCode(xdmlSmall);
		String monitorTime = (String) smallData.get("MONITORTIME");
		SimpleDateFormat sdf = new SimpleDateFormat(DamConstant.SDF_YMD_HMS);
		if (null == lastMonitorTime ) {
			log.debug("-------------数据未更新("+xdmlSmall+")----上次更新时间:" + lastMonitorTime + "  此次更新时间:" + monitorTime);
			lastMonitorTime = monitorTime;
			return null;
		}else if (sdf.parse(monitorTime).getTime() - sdf.parse(lastMonitorTime).getTime() > 0) {
			log.debug("-------------数据已经更新("+xdmlSmall+")----上次更新时间:" + lastMonitorTime + "  此次更新时间:" + monitorTime);
			lastMonitorTime = monitorTime;
		}else {
			log.debug("-------------数据未更新("+xdmlSmall+")----上次更新时间:" + lastMonitorTime + "  此次更新时间:" + monitorTime);
			return null;
		}

		Integer resultPm10 = smallData.get("PM10") == null ? 0 : (Integer) smallData.get("PM10");
		Integer resultPm25 = smallData.get("PM25") == null ? 0 : (Integer) smallData.get("PM25");
		Integer resultSo2 = smallData.get("SO2") == null ? 0 : (Integer) smallData.get("SO2");
		Integer resultNo2 = smallData.get("NO2") == null ? 0 : (Integer) smallData.get("NO2");
		Double resultCo = smallData.get("CO") == null ? 0 : (Double) smallData.get("CO");
		Integer resultO3 = smallData.get("O3") == null ? 0 : (Integer) smallData.get("O3");
		Double smallTemp = smallData.get("TEMP") == null ? 0.0 : (Double) smallData.get("TEMP");
		Double smallHum = smallData.get("HUM") == null ? 0.0 : (Double) smallData.get("HUM");
//		log.debug("-------------------推送西大明路,前查询小型站数据("+xdmlSmall+")-----------------------" + monitorTime);
//		log.debug("PM10:" + smallPm10 + " |PM2.5:" + smallPm25 + "|SO2:" + smallSo2 + "|NO2:"+ smallNo2 + "|O3:" + smallO3+ "|CO:" + smallCo + "|TEMP:"+smallTemp+"|HUM:" + smallHum);
//		log.debug("--------------------------------------------------------------------");

		log.debug("-------------------推送西大明路,校准后数据-----------------------");
		log.debug("PM10:" + resultPm10 + " |PM2.5:" + resultPm25 + "|SO2:" + resultSo2 + "|NO2:"+ resultNo2 + "|O3:" + resultO3+ "|CO:" + resultCo);
		log.debug("--------------------------------------------------------------------");

		// 获取日历实例并赋予当前时间
		Calendar cNow = Calendar.getInstance();
		// 获取日历实例并赋予当前时间
		String[] weeks = new String[]{"日","一","二","三","四","五","六"};
		if(!"".equals(monitorTime)){
			cNow.setTime((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(monitorTime));
		}

		monitorTime = cNow.get(Calendar.DATE)+"日"+cNow.get(Calendar.HOUR_OF_DAY)+"时";

		EnvDataPO curEnvDataPO = new EnvDataPO();

		curEnvDataPO.setPM10Concentration(resultPm10+"μg");
		curEnvDataPO.setPM2_5Concentration(resultPm25+"μg");
		curEnvDataPO.setSO2Concentration(resultSo2 + "μg");
		curEnvDataPO.setNO2Concentration(resultNo2+"μg");
		curEnvDataPO.setO3Concentration(resultO3+"μg");
		curEnvDataPO.setCOConcentration(resultCo+"mg");
		curEnvDataPO.setPubDate(monitorTime);
		curEnvDataPO.setTodayTemperature(smallTemp+"°C");

		cNow = Calendar.getInstance();
		// 发布时间
		curEnvDataPO.setDayAndWeek(new SimpleDateFormat("MM月dd日").format(cNow.getTime())+" 星期" + weeks[cNow.get(Calendar.DAY_OF_WEEK)-1]);

		return curEnvDataPO;
	}


}
